﻿using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Portal.Data.Entity;
using log4net;
using log4net.Config;

namespace Converter
{
    public partial class MainForm : Form
    {
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public MainForm()
        {
            InitializeComponent();

            XmlConfigurator.Configure();
        }
        private void btnContent_Click(object sender, EventArgs e)
        {
            msg.Text = "start building content...";
            var t = new Thread(BuildContent);
            t.Start();
        }
        void BuildContent()
        {
            try
            {
//                using (var context = new CmsDataContext())
//                {
//                    int id = 0;
//                    while (true)
//                    {
//                        int id1 = id;
//                        var all = context.Contents.Where(x => x.MoveBody == 0 && x.Content_ID > id1).Take(100).ToList();
//                        if (all.Count > 0)
//                        {
//                            id = all[all.Count - 1].ID;
//                            foreach (var item in all)
//                            {
//                                if (!string.IsNullOrEmpty(item.Body))
//                                {
//                                    var mobileData = Content.ConvertToMobileBody(item.Body);
//                                    Content.SetBody(item.Book_ID, item.ID, item.Body, false);
//                                    Content.SetBodyMobile(item.Book_ID, item.ID, mobileData, false);
//                                }
//                                context.ExecuteCommand("update Content set MoveBody=1,Body=null Where Content_ID={0}", item.ID);
//                            }
//                        }
//                        else
//                        {
//                            break;
//                        }
//                    }
//                }
//                Logger.Info("Xong Content");
//                MessageBox.Show("Xong Content");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }
    }
}
